package com.java.zhuiyun.api.home.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.java.zhuiyun.api.home.entity.FrontDeskUser;
import com.java.zhuiyun.api.home.mapper.HomeMapper;
import com.java.zhuiyun.api.home.model.Model;
import com.java.zhuiyun.api.home.model.ModelOrder;
import com.java.zhuiyun.api.home.model.SalesVolumeYear;
import com.java.zhuiyun.api.home.service.HomeService;
import com.java.zhuiyun.api.order.entity.Course;
import com.java.zhuiyun.common.response.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName HomeServiceImpl
 * @Description TODO
 * @Author wuwan
 * @Date 2023/8/14 20:06
 **/
@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private HomeMapper homeMapper;
    /*
    * @Author wuwan
    * @Description 地区人数分布统计
    * @Date 2023/8/20 19:10
    * @Return
    * @param null
    */
    @Override
    public CommonResult statisticsOne() {
        List<FrontDeskUser> frontDeskUsers = homeMapper.statisticsOne();
        // 用来存储数据一起返回
        List<Model> resultList = new ArrayList<>();
        for (int i = 0; i < frontDeskUsers.size(); i++) {
            FrontDeskUser frontDeskUser = frontDeskUsers.get(i);
            String region = frontDeskUser.getRegion();
            Model frontDeskUsers1 = homeMapper.statisticsTwo(region);
            resultList.add(frontDeskUsers1);
        }
        if (resultList.size() == frontDeskUsers.size()) {
            return CommonResult.ok(resultList);
        } else {
            return CommonResult.error("未获取到地区信息");
        }
    }
    /*
    * @Author wuwan
    * @Description 月销量统计
    * @Date 2023/8/20 19:10
    * @Return
    * @param null
    */
    @Override
    public CommonResult salesVolume(String selectedYear) {
        //创建容器存储
        List<String> qwe = new ArrayList<>();
        List<Integer> qqq = new ArrayList<>();
        // 一年12个月循环12次
        for (int i = 1; i <= 12; i++) {
            // 查询每个月的销量
            ModelOrder modelOrder = homeMapper.salesVolumes(String.valueOf(i),selectedYear);
            // 拿出月份和销量
            Integer eng = modelOrder.getPrice();
            qwe.add(i+"月");
            qqq.add(eng);
        }
        // 将两个数组返回
        return CommonResult.ok(CollUtil.newArrayList(qwe, qqq));
    }
    /*
    * @Author wuwan
    * @Description 年销量
    * @Date 2023/9/6 16:36
    * @Return
    * @param null
    */
    @Override
    public CommonResult salesVolumesYear() {
        List<SalesVolumeYear> salesVolumeYears = homeMapper.salesVolumesYear();
        return CommonResult.ok(salesVolumeYears);
    }
    /*
    * @Author wuwan
    * @Description 年盈利额度
    * @Date 2023/9/6 16:36
    * @Return
    * @param null
    */
    @Override
    public CommonResult getgetqq() {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("getgetqq", homeMapper.getgetqq().getPrice());
        resultMap.put("salesVolumeYear", homeMapper.salesVolumesYearss().getValue());
        resultMap.put("getmember", homeMapper.getmember().getPrices());
        resultMap.put("getgetmenber", homeMapper.getgetmenber().getValuess());
        return CommonResult.ok(resultMap);
    }

    @Override
    public CommonResult courseRanking() {
        List<Course> list = new ArrayList<>();
        List<SalesVolumeYear> salesVolumeYear = homeMapper.courseRanking();
        for (int i = 0; i < salesVolumeYear.size(); i++) {
            SalesVolumeYear salesVolumeYear1 = salesVolumeYear.get(i);
            String name = salesVolumeYear1.getName();
            Course courseName = homeMapper.getCourseName(Integer.parseInt(name));
            courseName.setPrice(salesVolumeYear1.getPrice());
            courseName.setValue(salesVolumeYear1.getValue());
            list.add(courseName);
        }
        return CommonResult.ok(list);
    }

}